package org.diysearch.helpers;

import java.util.List;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Options;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.diysearch.dao.filters.LinkFilter;
import org.diysearch.dao.interfaces.LinkDao;
import org.diysearch.exceptions.LinkCommandNotFound;
import org.diysearch.models.Link;
import org.diysearch.tools.BaseTool;

public abstract class LinkHelper {
	private static final Log LOG = LogFactory.getLog(LinkHelper.class);
	private static final LinkDao dao = (LinkDao) BaseTool.IOC_ENGINE.getBean("linkDao");
	private static Options olist = new Options();
	private static Options os = new Options();
	
	static {
		olist.addOption("cat",true,"get by category");
		olist.addOption("item",true,"get by item");
		olist.addOption("tag",true,"tag name");
		olist.addOption("ft",true,"get by filter");
		olist.addOption("st",true,"get by status");
		olist.addOption("jst",true,"just get by status");
		olist.addOption("usr",true,"get by user");
		olist.addOption("rt",true,"get by rating");
		olist.addOption("ep",false,"get editor picks");
		olist.addOption("lmt",true,"limit the number of links");
		
		os.addOption("ii",true,"increment impressions");
		os.addOption("ict",true,"increment click thrus");
		os.addOption("ep",true,"set as editor pick");
		os.addOption("dep",true,"unset as editor pick");
		os.addOption("ur",true,"up rating");
		os.addOption("dr",true,"down rating");
		os.addOption("sa",true,"set as active");
		os.addOption("sia",true,"set as inactive");
	}
	
	public static int deleteLinkByOwner (int owner) throws Exception {
		Link l = new Link();
		l.setOwner(owner);
		return dao.delete(l);
	}
	
	public static int deleteLinkById (int id) throws Exception {
		Link l = new Link();
		l.setId(id);
		return dao.delete(l);
	}
	
	public static int save (Link link) throws Exception {
		return dao.save(link);
	}
	
	public static int save(List<Link> links) throws Exception {
		return dao.save(links);
	}
	
	public static List<Link> fetchLinks (String[] args) 
		throws Exception 
	{
		if (LOG.isDebugEnabled()) {
			for (String arg : args) {
				LOG.debug("Argument: " + arg);
			}
		}
		CommandLineParser parser = new BasicParser();
		CommandLine cmd = parser.parse(olist, args);
		LinkFilter filter = new LinkFilter();
		if (! cmd.hasOption("st") && ! cmd.hasOption("jst")) {
			// we'll just default status to 1, the safest choice
			filter.setStatus(1);
		} else if (cmd.hasOption("st") && ! cmd.hasOption("jst")) {
			filter.setStatus(Integer.parseInt(cmd.getOptionValue("st")));
		}
		if (cmd.hasOption("cat")) {
			int catId = Integer.parseInt(cmd.getOptionValue("cat"));
			filter.setCategoryId(catId);
			return dao.findByCategory(filter);
		} else if (cmd.hasOption("tag")) {
			String tag = cmd.getOptionValue("tag");
			filter.setTag(tag);
			return dao.findByTag(filter);
		} else if (cmd.hasOption("jst")) {
			int status = Integer.parseInt(cmd.getOptionValue("jst"));
			return dao.findByStatus(status);
		} else if (cmd.hasOption("ft")) {
			String ft = cmd.getOptionValue("ft");
			filter.setFilter(ft);
			return dao.findByFilter(filter);
		} else if (cmd.hasOption("usr")) {
			int uid = Integer.parseInt(cmd.getOptionValue("usr"));
			filter.setOwner(uid);
			return dao.findByOwner(filter);
		} else if (cmd.hasOption("rt")) {
			int rating = Integer.parseInt(cmd.getOptionValue("rt"));
			int limit = BaseTool.CONFIG_BEAN.getPageLimit(); // default limit
			if (cmd.hasOption("lmt")) {
				limit = Integer.parseInt(cmd.getOptionValue("lmt"));
			}
			filter.setLimit(limit);
			filter.setRating(rating);
			return dao.findByRating(filter);
		} else if (cmd.hasOption("ep")) {
			return dao.findByEditorPick(filter);
		} else {
			throw new Exception ("You must provide some argument for what links you want to fetch!");
		}
	}
	public static Link fetchLink(int id, int status) throws Exception {
		return dao.findById(id,0);
	}
	public static Link fetchLink (int id) throws Exception {
		return dao.findById(id, 0);
	}
	
	public static int setStats (String[] args) throws Exception {
		CommandLineParser parser = new BasicParser();
		CommandLine cmd = parser.parse(os, args);
		Link link = new Link();

		if (cmd.hasOption("ii")) {
			link.setId(Integer.parseInt(cmd.getOptionValue("ii")));
			return dao.incrementImpression(link);
		} else if (cmd.hasOption("ict")) {
			link.setId(Integer.parseInt(cmd.getOptionValue("ict")));
			return dao.incrementClickthru(link);
		} else if (cmd.hasOption("ep")) {
			link.setId(Integer.parseInt(cmd.getOptionValue("ep")));
			link.setEditorPick(1);
			return dao.setAsEditorPick(link);
		} else if (cmd.hasOption("dep")) {
			link.setId(Integer.parseInt(cmd.getOptionValue("dep")));
			link.setEditorPick(0);
			return dao.setAsEditorPick(link);
		} else if (cmd.hasOption("ur")) {
			link.setId(Integer.parseInt(cmd.getOptionValue("ur")));
			return dao.incrementRating(link);
		} else if (cmd.hasOption("dr")) {
			link.setId(Integer.parseInt(cmd.getOptionValue("dr")));
			return dao.deincrementRating(link);
		} else if (cmd.hasOption("sa")) {
			link.setId(Integer.parseInt(cmd.getOptionValue("sa")));
			link.setIsActive(1);
			return dao.setAsActive(link);
		} else if (cmd.hasOption("sia")) {
			link.setId(Integer.parseInt(cmd.getOptionValue("sia")));
			link.setIsActive(0);
			return dao.setAsActive(link);
		} else {
			throw new LinkCommandNotFound("Could not understand your command: " + cmd.getArgs());
		}
		
	}
	
	public static int move(int catId, int newCatId) throws Exception {
		LinkFilter filter = new LinkFilter();
		filter.setCategoryId(catId);
		filter.setNewCategoryId(newCatId);
		return dao.move(filter);
	}
}
